/*meir grinstein*/

#define _GNU_SOURCE

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <unistd.h>

#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>

#include "ftp.h"

#define IP "127.0.0.1"
#define PORT 7
#define MAX_MESSAGE 100
#define SENT_MESSAGE_SIZE 50
#define MAX_CLINTS 20
#define TIMES 30

void* server(void *ptr)
{
    FtpServer *ftpServer = FTPServerNew(PORT);

    FTPServerAddUser(ftpServer, "meir", "1234");

    FTPServerRun(ftpServer);
    FTPServerDel(ftpServer);

    puts("ftp server finished");

    return 0;
}

void* client(void *ptr)
{
    FtpClient *ftpClient;

    ftpClient = FTPClientNew();
    FTPClientConnect(ftpClient, IP, PORT);

    FTPClientExec(ftpClient, "user moshe");
    FTPClientExec(ftpClient, "user meir");

    FTPClientExec(ftpClient, "pass 1vhcfg4");
    FTPClientExec(ftpClient, "pass 1364");
    FTPClientExec(ftpClient, "pass 1234");

    FTPClientExec(ftpClient, "bye");
    
    FTPClientExec(ftpClient, "cd 1234");
    FTPClientExec(ftpClient, "cd meir_files");
    
    FTPClientExec(ftpClient, "ls");
    
    FTPClientExec(ftpClient, "bye");

    FTPClientExec(ftpClient, "user moshe");
    FTPClientExec(ftpClient, "user meir");

    FTPClientDel(ftpClient);

    return 0;
}


int main(void)
{
    pthread_t thread[MAX_CLINTS+1];
    int  iret[MAX_CLINTS+1], i;

    /* Create independent threads each of which will execute function */

    iret[0] = pthread_create( &thread[0], NULL, server, "message from server");
    /*iret[1] = pthread_create( &thread[1], NULL, TcpClient, "message from client1");
    iret[2] = pthread_create( &thread[2], NULL, TcpClient, "message from client2");
    iret[3] = pthread_create( &thread[3], NULL, TcpClient, "message from client3");
    iret[4] = pthread_create( &thread[4], NULL, TcpClient, "message from client4");*/
    iret[1] = pthread_create( &thread[1], NULL, client, "message from client1");
    /*iret[2] = pthread_create( &thread[2], NULL, client, "message from client2");
    iret[3] = pthread_create( &thread[3], NULL, client, "message from client3");
    iret[4] = pthread_create( &thread[4], NULL, client, "message from client4");*/

    /* Wait till threads are complete before main continues. Unless we  */
    /* wait we run the risk of executing an exit which will terminate   */
    /* the process and all threads before the threads have completed.   */

    for (i=0; i<=4; ++i)
    {
        pthread_join( thread[i], NULL);
    }

    for (i=0; i<=4; ++i)
    {
        printf("Thread %d returns: %d\n", i, iret[i]);
    }

    return 0;
}
